package com.stxy.util;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends Frame {
	private Image iBuffer; // 双缓冲绘图
	private Graphics gBuffer; // 双缓冲绘图

	/**
	 * 加载窗口
	 */
	public void launchFrame() {
		setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
		setLocation(100, 100);
		setVisible(true);

		new PaintThread().start(); // 启动重画线程

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}
	public void update(Graphics g) { // 双缓冲绘图
		if (iBuffer == null) {
			iBuffer = createImage(this.getSize().width, this.getSize().height);
			gBuffer = iBuffer.getGraphics();
		}
		gBuffer.setColor(getBackground());
		gBuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
		paint(gBuffer);
		g.drawImage(iBuffer, 0, 0, this);
	}

	/**
	 * 定义一个重画窗口的线程类，是一个内部类
	 * 
	 * @author dell
	 * 
	 */
	class PaintThread extends Thread {

		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(40); // 1s = 1000ms
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}
}
